<template>
    <div>
        <el-table :data="userlist" border style="width: 100%">
            <el-table-column prop="uid" label="用户编号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="用户名称" width="180">
            </el-table-column>
            <el-table-column prop="rolename" label="所属角色"> </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        <span v-if="item.row.status == 1">正常</span>
                        <span v-else>禁用</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button class="btnInfo" type="primary" icon="el-icon-edit" size="mini" circle @click="edit(item.row.uid)"></el-button>
                        <el-button class="btnInfo" type="danger" icon="el-icon-delete" size="mini" circle
                            @click="del(item.row.uid)"></el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" :page-size="getSize" :total="total"
            @current-change="changePage">
        </el-pagination>
    </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { userDelete } from '../../request/api'
export default {
    data() {
        return {
            tableData: []
        };
    },
    computed: {
        ...mapGetters({
            userlist: 'user/getUser',
            total: 'user/getCount',
            getSize: 'user/getSize'
        })
    },
    mounted() {
        //页面一加载获取管理员列表
        this.getUser()
        //总条数的获取
        this.getCount()
    },
    methods: {
        ...mapActions({
            getUser: 'user/getUserAction',
            getCount: 'user/getCountAction',
            changePageAction: 'user/changePageAction'
        }),
        changePage(n) {
            console.log(n);
            this.changePageAction(n)
        },
        // 删除事件
        del(uid) {
            this.$confirm("你确定要删除这条数据吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    // 调用删除接口
                    userDelete({ uid }).then((res) => {
                        if (res.data.code == 200) {
                            this.$message.success(res.data.msg);
                            //删除成功之后我们要重新调用列表以及总条数
                            this.getUser()
                            this.getCount()
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    })
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                })
        },
        // 点击编辑
        edit(uid){
            this.$emit('edit',uid)
        }
    }
};
</script>

<style  lang="" scoped>

</style> 